package com.xhh.nphm.web.controller.system;

import java.util.Date;
import java.util.List;

import com.xhh.nphm.common.annotation.Log;
import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.core.domain.AjaxResult;
import com.xhh.nphm.common.core.domain.entity.SysUser;
import com.xhh.nphm.common.core.page.TableDataInfo;
import com.xhh.nphm.common.enums.BusinessType;
import com.xhh.nphm.common.utils.ShiroUtils;
import com.xhh.nphm.system.domain.TwfSendBackEty;
import com.xhh.nphm.system.domain.Twfurgent;
import com.xhh.nphm.system.domain.TworkFormBaseEty;
import com.xhh.nphm.system.domain.TworkFormLogEty;
import com.xhh.nphm.system.service.ITwfurgentService;
import com.xhh.nphm.system.service.ITworkFormLogService;
import com.xhh.nphm.system.service.IWfSendBackService;
import com.xhh.nphm.system.service.IWorkFormBaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 电话派单催办Controller
 *
 * @date 2022-05-10
 */
@Controller
@RequestMapping("/system/twfurgent")
public class TwfurgentController extends BaseController {
    private String prefix = "system/twfurgent";


    @Autowired
    private ITwfurgentService twfurgentService;
    @Autowired
    private IWfSendBackService wfSendBackService;
    @Autowired
    private ITworkFormLogService iTworkFormLogService;
    @Autowired
    private IWorkFormBaseService workFormBaseService;


    /**
     * 新增电话派单催办
     */
    @GetMapping("/twfurgentAdd/{pkId}")
    public String twfurgentAdd(@PathVariable Integer pkId,ModelMap map) {
        map.put("sendId",pkId);
        return prefix + "/twfurgentAdd";
    }

    /**
     * 新增保存电话派单催办
     */
    @Log(title = "电话派单催办", businessType = BusinessType.INSERT)
    @PostMapping("/twfurgentAddJson")
    @ResponseBody
    public AjaxResult twfurgentAddJson(Twfurgent twfurgent) {
        SysUser user = ShiroUtils.getSysUser();
        TwfSendBackEty twfSendBackEty = wfSendBackService.getById(twfurgent.getSendId());
        if (twfSendBackEty !=null) {
            twfurgent.setSdeptId(twfSendBackEty.getSdeptId());
            twfurgent.setSdeptName(twfSendBackEty.getSdeptName());
            twfurgent.setSuserName(twfSendBackEty.getSuserName());
            twfurgent.setSuserNo(twfSendBackEty.getSuserNo());
            twfurgent.setSbdeptId(twfSendBackEty.getSbdeptId());
            twfurgent.setSbdeptName(twfSendBackEty.getSbdeptName());
            twfurgent.setWfId(twfSendBackEty.getWfId());
            twfurgent.setWorkFormNo(twfSendBackEty.getMyTwfNO());
            twfurgent.setBackLimit(twfSendBackEty.getBackLimit());
        }
        TworkFormBaseEty tworkFormBaseEty = workFormBaseService.getById(twfSendBackEty.getWfId());
        if (tworkFormBaseEty !=null) {
            twfurgent.setWorkFormNo(tworkFormBaseEty.getWorkFormNo());
        }
        twfurgent.setMsgTime(new Date());
        int row = 0;
        boolean save = twfurgentService.save(twfurgent);
        if (save) {
            row = 1;
            TworkFormLogEty tworkFormLogEty = new TworkFormLogEty();
            tworkFormLogEty.setOpDeptNo(user.getDeptId().toString());
            tworkFormLogEty.setOpDeptName(user.getDeptName());
            tworkFormLogEty.setOpUserNo(user.getUserNo());
            tworkFormLogEty.setOpUserName(user.getUserName());
            tworkFormLogEty.setEffectFlag(1);
            tworkFormLogEty.setWsOpen(1);
            tworkFormLogEty.setWsDesc("催办");
            tworkFormLogEty.setOpAction("催办");
            tworkFormLogEty.setOpBtnId(-1);
            tworkFormLogEty.setOpDesc("催办");
            iTworkFormLogService.save(tworkFormLogEty);
        }
        return toAjax(row);
    }

}
